package com.zmzncs.lmtc.module.park.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.zmzncs.lmtc.common.util.ExceptionUtil;
import com.zmzncs.lmtc.module.park.dao.ManagerExpandDao;
import com.zmzncs.lmtc.module.park.dao.ManagerLinkDao;
import com.zmzncs.lmtc.module.park.pojo.manager_expand.ManagerExpand;
import com.zmzncs.lmtc.module.park.pojo.manager_link.ManagerLink;
import com.zmzncs.lmtc.module.park.pojo.park_info.ParkInfo;
import org.springframework.beans.BeanUtils;
import com.zmzncs.lmtc.module.park.pojo.park_manager.*;
import com.zmzncs.lmtc.module.park.dao.ParkManagerDao;
import com.zmzncs.lmtc.module.park.service.ParkManagerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * <p>
 * 车场管理员表 服务实现类
 * </p>
 *
 * @author 高博文
 * @since 2020-12-09
 */
@Service
public class ParkManagerServiceImpl extends ServiceImpl<ParkManagerDao, ParkManager> implements ParkManagerService {

    @Autowired
    ParkManagerDao parkManagerDao;
    @Autowired
    ManagerExpandDao managerExpandDao;
    @Autowired
    ManagerLinkDao managerLinkDao;

    @Override
    public IPage<ParkManagerDTO> page(ParkManagerSearchForm form) {
        IPage<ParkManagerDTO> iPage = parkManagerDao.list(new Page(form.getCurrPage(),form.getPageSize()));
        if (iPage != null){
            List<ParkManagerDTO> records = iPage.getRecords();
            if (records != null && records.size() > 0){
                for (ParkManagerDTO record : records) {
                    List<ParkInfo> parkInfoList = managerLinkDao.getParkInfoByParkManageId(record.getId());
                    record.setParkInfoList(parkInfoList);
                }
            }
        }
        return iPage;
    }

    @Override
    @Transactional
    public void add(ParkManagerAddForm form){
        ParkManagerDTO managerDTO = parkManagerDao.getByMobile(form.getMobilePhone());
        if (managerDTO != null){
            ExceptionUtil.businessException("手机号已存在，请更换手机号");
        }
        ParkManager parkManager = new ParkManager();
        BeanUtils.copyProperties(form, parkManager);
        parkManager.setIsOwner(1);
        parkManager.setIsDeleted(0);
        parkManager.setStatus(1);
        parkManager.setCreateTime(new Date());
        parkManagerDao.insert(parkManager);

        ManagerExpand managerExpand = new ManagerExpand();
        BeanUtils.copyProperties(form, managerExpand);
        managerExpand.setParkManagerId(parkManager.getId());
        managerExpand.setCreateTime(new Date());
        managerExpandDao.insert(managerExpand);
    }

    @Override
    public void delete(Long id){
        ParkManager parkManager = new ParkManager();
        parkManager.setId(id);
        parkManager.setIsDeleted(1);

        parkManagerDao.updateById(parkManager);
    }

    @Override
    @Transactional
    public void update(ParkManagerUpdateForm form){
        ParkManager parkManager = new ParkManager();
        BeanUtils.copyProperties(form, parkManager);
        parkManagerDao.updateById(parkManager);

        LambdaQueryWrapper<ManagerLink> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ManagerLink::getParkManagerId,form.getId());
        managerLinkDao.delete(queryWrapper);

        List<Long> parkIdList = form.getParkIdList();
        if (parkIdList != null && parkIdList.size() > 0){
            for (Long parkId : parkIdList) {
                ManagerLink link = new ManagerLink();
                link.setParkId(parkId);
                link.setParkManagerId(form.getId());
                managerLinkDao.insert(link);
            }
        }
    }

    @Override
    public ParkManagerDTO getById(Long id){
        ParkManagerDTO parkManagerDTO = parkManagerDao.getById(id);

        return parkManagerDTO;
    }

    @Override
    public ParkManagerDTO getByMobile(String mobilePhone) {
        ParkManagerDTO parkManagerDTO = parkManagerDao.getByMobile(mobilePhone);
        return parkManagerDTO;
    }

    @Override
    public void deleteByManagerId(Long id) {
        parkManagerDao.deleteByManagerId(id);
    }

}
